{% import "card_macros.html" as card_macros %}

{% macro card_detail(card, request) -%}
    {% set vs_goal = card.cycle_vs_goal %}
    <div class="card_on_board {% if vs_goal == 2 %}cycle_double_over_goal{% elif vs_goal == 1 %}cycle_over_goal{% elif vs_goal == 0 %}cycle_in_goal{% endif %} card_type_{{ card.type|slugify }} {% if card.blocked %}card_blocked{% endif %}" id="card_{{ card.key }}">
        <div class="card_key"><span>{{ card.key }}</span>
            {% if card.done_date %}
                - {{ card.cycle_time }}d / {{ card.cycle_goal.upper }}d
            {% elif card.start_date %}
                - {{ card.current_cycle_time() }}d / {{ card.cycle_goal.upper }}d
            {% endif %}
            {% if not card.is_card %}<i class="icon-plus-sign-alt" title="{{ card.type }}"></i>{% endif %}
            {% if 'Expedite' in card.service_class['name'] %}<i class="icon-forward" title="{{ card.service_class['name'] }}"></i>
            {% elif 'Date' in card.service_class['name'] %}<i class="icon-calendar" title="{{ card.service_class['name'] }}"></i>{% endif %}
        </div>

        <div class="card_contents">
        <div class="title">
            <p class="title"><a href="{{ url_for('card', key=card.key) }}">{{ card.title }}</a></p>

            {% if card.blocked %}
            <div class="blocker">
                <p>{{ card_macros.hours_to_days(card.blockers[-1].duration) }} - {{ card.blockers[-1].reason }}</p>
            </div>
        {% endif %}

        </div>


        <p class="assignee">
            {{ card_macros.hours_to_days(card.time_in_state) }} <br />
            {% if card.due_date %}Due: {{ card.due_date.strftime("%m/%d") }}{% endif %}
            {% if card.service_class['name'] in ['Intangible', 'Not Prioritized'] %}
                {{ card.service_class.get('name') }}
                {% if card.worked_on %} &ndash; {% endif %}
            {% endif %}

            {% if card.state not in ["Backlog", "Done"] %}
                {% if card.worked_on %}
                    {% for worker in card.worked_on %}
                        <a href="{{ url_for('person', name=worker) }}">{{ worker }}</a>{% if not loop.last %},{% endif %}
                    {% endfor %}
                {% endif %}
            {% endif %}
        </p>
        </div>

    </div>
{%- endmacro %}

{% macro board(board, wip_limits, request, repeat_headers=False, weekly_throughput=None, backlog_markers=None) -%}
<table class="board">
<tr>
  {% for header in board.headers %}
    {% if header.get('state', '') not in ('Ready: Testing', 'Build to OTIS', 'Prodward Bound') %}
    <th class="{{ header.get('state', header.get('label', ''))|slugify }}">
        {% if header.get('label') %}
            {{ header.label }}
        {% else %}
            {% if wip_limits.get(header.state) %}
                {% set col_wip_limit = wip_limits.get(header.state) %}
                {% if header.count > col_wip_limit %}
                    <!-- header.count: {{ header.count }} > col_wip_limit: {{ col_wip_limit }} -->
                    {% set col_classname = "col_over_wip" %}
                {% else %}
                     {% set col_classname = "col_normal" %}
                {% endif %}
            {% else %}
                {% set col_wip_limit = None %}
                {% set col_classname = "col_normal" %}
            {% endif %}
            <div class="{{ col_classname }}">{{ header.state.replace("Backlog", "Ready: Elabo").replace("OTIS", "TIE") }} ({{ header.count }}{% if col_wip_limit %}/{{ col_wip_limit }}{% endif %})</div>
        {% endif %}
        {% if header.get('state', '') in ('Building', 'Testing', 'OTIS Verify') %}
            {% if wip_limits.get(board.headers[loop.index0+1].state) %}
                {% set col_wip_limit = wip_limits.get(board.headers[loop.index0+1].state) %}
                {% if board.headers[loop.index0+1].count > col_wip_limit %}
                    {% set col_classname = "col_over_wip" %}
                {% else %}
                     {% set col_classname = "col_normal" %}
                {% endif %}
            {% else %}
                {% set col_wip_limit = None %}
                {% set col_classname = "col_normal" %}
            {% endif %}
            <div>{{ board.headers[loop.index0+1].state.replace("Backlog", "Ready: Elabo").replace("OTIS", "TIE") }} ({{ board.headers[loop.index0+1].count }}{% if col_wip_limit %}/{{ col_wip_limit }}{% endif %})</div>
        {% endif %}

    </th>
    {% endif %}
  {% endfor %}
</tr>

{% for row in board %}

{% if repeat_headers and not loop.first %}
<tr>
  {% for header in board.headers %}
    {% if header.get('state', '') not in ('Ready: Testing', 'Build to OTIS', 'Prodward Bound') %}
    <th class="{{ header.get('state', header.get('label', ''))|slugify }}">
        {% if header.get('label') %}
            {{ header.label }}
        {% elif header.get('state', '') in ('Building', 'Testing', 'OTIS Verify') %}
            {{ header.state.replace("Backlog", "Ready: Elabo").replace("OTIS", "TIE") }}
        {% else %}
            {{ header.state.replace("Backlog", "Ready: Elabo").replace("OTIS", "TIE") }}
        {% endif %}

    </th>
    {% endif %}
  {% endfor %}
</tr>
{% endif %}


<tr>
{% for cell in row %}
    {% if cell.get('label') %}
        <td class="team">{{ cell['label'] }}</td>
    {% elif cell.get('state', '') == "Backlog" %}
        <td class="cards cards_{{ cell.get('state')|slugify }}">
            {% for card in cell['cards'] %}
                {% if backlog_markers %}
                    {%- if loop.index0 % weekly_throughput == 0 %}
                        <div class="split_cell_header ui-state-default ui-state-disabled">
                            Roughly: Week of {{ backlog_markers.pop(0).strftime("%m/%d") }}
                        </div>
                    {% endif %}
                {% endif %}
                {{ card_detail(card, request) }}
            {% endfor %}

            <h3><a href="./backlog/">Full backlog &raquo;</a></h3>
        </td>
    {% elif cell.get('state', '') not in ('Ready: Testing', 'Build to OTIS', 'Prodward Bound') %}
        <td class="cards cards_{{ cell.get('state')|slugify }}">
        {% for card in cell['cards'] %}
                {{ card_detail(card, request) }}
        {% endfor %}
        {% if cell.get('state', '') in ('Building', 'Testing', 'OTIS Verify') %}
            {% for card in row[loop.index0+1]['cards'] %}
                {% if loop.first %}
                    <div class="split_cell_header">
                        {{ card.state }}
                    </div>
                {% endif %}
                    {{ card_detail(card, request) }}
            {% endfor %}
        {% endif %}
        </td>
    {% endif %}
{% endfor %}

</tr>
{% endfor %}

</table>

{%- endmacro %}
